package cn.demoncat.util.iching;

import cn.demoncat.util.exception.ParamRuntimeException;
import org.apache.commons.lang3.StringUtils;

/**
 * 易经工具
 *
 * @author 延晓磊
 * @since 2021年07月13日
 */
public class IChingUtil {

    /**
     * 转汉字
     *
     * @param number    "111111"，从下至上
     * @return "阳阳阳阳阳阳"
     */
    public static String toChinese(String number){
        StringBuilder result = new StringBuilder();
        for (char c : number.toCharArray() ) {
            result.append(c == '1' ? "阳" : "阴");
        }
        return result.toString();
    }

    /**
     * 转数字
     *
     * @param chinese    "阳阳阳阳阳阳"，从下至上
     * @return "111111"
     */
    public static String toNumber(String chinese){
        StringBuilder result = new StringBuilder();
        for (char c : chinese.toCharArray() ) {
            result.append(c == '阳' ? "1" : "0");
        }
        return result.toString();
    }

    /**
     * 获取卦名
     *
     * @param mark  卦象："111111" || "阳阳阳阳阳阳"，从下至上
     * @return "乾"
     */
    public static String get(String mark){
        if (StringUtils.isBlank(mark) || mark.length() != 6) {
            throw new ParamRuntimeException("请输入正确的卦象");
        }
        // 转为数字型
        if (mark.charAt(0) != '1' && mark.charAt(0) != '0') {
            mark = toNumber(mark);
        }
        return IChingConstant.GUA64.get(mark);
    }

}
